package com.twentyOne.ClothingLand.dao.impl;

import java.util.List;

import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.Transaction;
import com.twentyOne.ClothingLand.dao.BaseDAO;
import com.twentyOne.ClothingLand.dao.ISizeDAO;
import com.twentyOne.ClothingLand.vo.Size;

public class SizeDAO extends BaseDAO implements ISizeDAO {

	@Override
	public void addSize(Size size) {
		
		Session session = getSession();
		Transaction tx = session.beginTransaction();
		session.save(size);
		tx.commit();
		session.close();
		
	}

	@Override
	public void deleteSize(Size size) {
		
		Session session = getSession();
		Transaction tx = session.beginTransaction();
		String hql = "delete Size as s where s.sizeId = "+size.getSizeId()+"";
		Query query = session.createQuery(hql);
		query.executeUpdate();
		tx.commit();
		session.close();
		
	}

	@Override
	public void modifySize(Size size) {
		
		Session session = getSession();
		Transaction tx = session.beginTransaction();
		String hql = "update Size set name=:name, description=:description where sizeId=:sizeId";
		Query query = session.createQuery(hql);
		query.setString("name", size.getName());
		query.setString("description", size.getDescription());
		query.setInteger("sizeId", size.getSizeId());
		query.executeUpdate();
		tx.commit();
		session.close();
		
	}

	@Override
	public List<Size> searchAllSize() {

		Session session = getSession();
		String hql = "from Size";
		Query query = session.createQuery(hql);
		List<Size> list = query.list();
		session.close();
		if(list.size() != 0)
			return list;
		else 
			return null;
		
	}

	@Override
	public Size searchByID(int sizeId) {
		
		Session session = getSession();
		String hql = "from Size as s where s.sizeId=" + sizeId;
		Query query = session.createQuery(hql);
		List<Size> list = query.list();
		session.close();
		if(list.size() != 0) {
			Size size = list.get(0);
			return size;
		}else {
			return null;
		}				
		
	}

}
